﻿using FRMS.BUS;
using FRMS.DAL;
using FRMS.Model;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FRMS.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class DataSyncController : ControllerBase
    {
        private readonly IHostingEnvironment _hostingEnvironment;
        public DataSyncController(IHostingEnvironment hostingEnvironment)
        {
            _hostingEnvironment = hostingEnvironment;
        }

        /// <summary>
        /// 同步业主信息
        /// </summary>
        [HttpPost]
        [Route("SyncOwnerInfo")]
        public ReturnMessage SyncOwnerInfo(SyncOwnerInfo ownerInfo)
        {
            ReturnMessage message = new ReturnMessage();
            OwnerBUS BUS = new OwnerBUS();
            try
            {
                message = BUS.AddSyncOwner(ownerInfo);
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "同步业主信息异常";
                message.data = null;
            }
            return message;
        }

        /// <summary>
        /// 查询所有房间信息
        /// </summary>
        [HttpPost]
        [Route("QuerRoomID")]
        public ReturnMessage QueryBuild(SyncBuildInfo buildInfo)
        {
            ReturnMessage message = new ReturnMessage();
            ProjectDAL projectDAL = new ProjectDAL();
            BuildBUS BUS = new BuildBUS();
            try
            {
                int projectID = projectDAL.QueryProjectID(buildInfo.projectcode);
                if (projectID > 0)
                {
                    message = BUS.QueryAllBuild(projectID);
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "项目不存在";
                }
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "查询楼栋房间异常";
                message.data = null;
            }
            return message;
        }

        /// <summary>
        /// 删除业主信息
        /// </summary>
        [HttpPost]
        [Route("DeleteOwnerInfo")]
        public ReturnMessage DeleteOwnerInfo(DeleteSyncOwnerInfo ownerInfo)
        {
            ReturnMessage message = new ReturnMessage();
            OwnerBUS BUS = new OwnerBUS();
            try
            {
                message = BUS.DeleteSyncOwner(ownerInfo);
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "删除业主信息异常";
                message.data = null;
            }
            return message;
        }

    }
}
